package com.buba.service.impl;

import com.buba.mapper.KudosMapper;
import com.buba.service.KudosService;
import entity.KudosEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: 孔德嘉
 * @Description: TODO点赞
 * @DateTime: 2023/3/8 10:42
 **/
@Service
public class KudosServiceImpl implements KudosService {
    //注入
    @Autowired
    private KudosMapper kudosMapper;
    /*
     * @param kudosEntity 点赞实体类
     * @return int
     * @author kdj
     * @describe:修改点赞状态
     * @date 2023/3/8 10:41
     */
    @Override
    public int updateKudosByUserId(KudosEntity kudosEntity) {
        int i =0;//定义i判断是否修改状态成功
        //查询该日记/话题是否点赞过
        KudosEntity kudosById = kudosMapper.getKudosById(kudosEntity.getKudosWholeId(),kudosEntity.getKudosDictionaries(),kudosEntity.getKudosUserId());
        //判断数据库中是否有  点赞过的记录
        if(kudosById!=null){
            //如果点赞过 并且没有取消点赞kudosWhether=0
            if(kudosById.getKudosWhether()==0){
                //则 修改kudos_whether为1 取消点赞
                kudosEntity.setKudosWhether(1);
                i = kudosMapper.upKudosNum(kudosEntity);
            }else{
                //如果点赞过 并且取消点赞了
                //则修改kudos_whether为0 点赞
                kudosEntity.setKudosWhether(0);
                i = kudosMapper.upKudosNum(kudosEntity);
            }
        }else{
            //如果没点赞过
            //则添加数据 并且kudos_whether为1 点赞
            //创建一个KudosEntity对象
            //添加到数据库中
            i = kudosMapper.addKudos(kudosEntity);
        }

        return i;
    }
}
